<?php
global $wpdb;
global $table_shoots,$table_eventtypes,$table_events, $table_ranges;
if($_REQUEST['action'] == 'addshoot')
{
	$query = $wpdb->prepare("insert into $table_shoots ($table_shoots.name, $table_shoots.rangeid) values (%s, %d)",
							$_REQUEST['shoots_name'],
							$_REQUEST['shoots_rangeid']);
	$result = $wpdb->query($query);
	if($result === FALSE)
	{
		die($wpdb->print_error());
	}
	$shootid = $wpdb->insertid;

	$eventtypes = $wpdb->get_results("select $table_eventtypes.name,$table_eventtypes.eventtypeid from $table_eventtypes");
	if($eventtypes === FALSE)
	{
		die($wpdb->print_error());
	}
	foreach($eventtypes as $eventtype)
	{
		if($eventtype->eventtypeid == 1)
		{
			$query = $wpdb->prepare("insert into $table_events (shootid,eventtypeid) values (%d,%d)", $shootid, $row['eventtypeid']);
			$result = $wpdb->query($query);
			if($result === FALSE)
			{
				die($wpdb->print_error());
			}
		}
	}
}
else if($_REQUEST['action'] == 'changeshoot')
{
	$query = $wpdb->prepare("update $table_shoots set $table_shoots.name=%s, $table_shoots.rangeid=%d where $table_shoots.shootid=%d",
								 $_REQUEST['shoots_name'],
								 $_REQUEST['shoots_rangeid'],
								 $_REQUEST['shoots_shootid']);
	$result = $wpdb->query($query);
	if($result === FALSE)
	{
		echo "Error: $wpdb->print_error()";
	}
}
else if($_REQUEST['action'] == 'remove')
{
	$query = $wpdb->prepare("delete from $table_shoots where $table_shoots.shootid=%d", $_REQUEST['shoots_shootid']);
	$result = $wpdb->query($query);
	if($result === FALSE)
	{
		echo "Error: $wpdb->print_error()";
	}
}
else if($_REQUEST['action'] == 'edit')
{
	$query = $wpdb->prepare("select $table_shoots.shootid, $table_shoots.name, $table_shoots.rangeid from $table_shoots where $table_shoots.shootid=%d", $_REQUEST['shoots_shootid']);
	$shoot = $wpdb->get_row($query);
	if($shoot === FALSE)
	{
		die($wpdb->print_error());
	}
	$ranges = $wpdb->get_results("select $table_ranges.rangeid, $table_ranges.name from $table_ranges");
	if($randes === FALSE)
	{
		die($wpdb->print_error());
	}
	$eventtypes = $wpdb->get_results("select $table_eventtypes.eventtypeid, $table_eventtypes.name from $table_eventtypes");
	if($eventtypes === FALSE)
	{
		die($wpdb->print_error());
	}
?>
<form action="admin.php?page=<?php print $GLOBALS['ts_path'] ?>/<?php echo basename(__FILE__); ?>" method="post">
<div class="wrap">
<h2>Edit Shoot:</h2>
Warning: For the time being, events do not get updated properly
<div>
<label for="name">Name:</label>
<input type="text" name="shoots_name" id="name" value="<?php echo $shoot->name; ?>" size="64" /><br />
<label for="range">Range:</label>
<select name="shoots_rangeid" id="range">
<?php
	foreach($ranges as $range)
	{
		if($range->rangeid == $shoot->rangeid)
		        echo "<option selected value=" . $range->rangeid . ">" . $range->name . "</option>\n";
    		else
			echo "<option value=" . $range->rangeid . ">" . $range->name . "</option>\n";
	}
?>
</select><br />
Events:<br/>
<?php
	foreach($eventtypes as $eventtype)
	{
		$query = $wpdb->prepare("select count(eventid) from events where events.shootid=%d and events.eventtypeid=%d",
    									$_REQUEST['shoots_shootid'],
    									$eventtype->eventtypeid);
    	$eventcount = $wpdb->get_var($query);
    	if($eventcount > 0)
    		echo "<input type=\"checkbox\" name=\"" . $eventtype->eventtypeid . "\" value=1 checked/>" . $eventtype->name . "<br />\n";
    	else
    		echo "<input type=\"checkbox\" name=\"" . $eventtype->eventtypeid . "\" value=1 />" . $eventtype->name . "<br />\n";
    }
?>
<input type="hidden" name="shoots_shootid" value="<?php echo $shoot->shootid ?>" />
<input type="hidden" name="action" value="changeshoot" />
</div>
<div class="submit"><input type="submit" value="Update Shoot &raquo;" /></div>
</div>
</form>
<?php
return;
}
?>

<form action="admin.php?page=<?php print $GLOBALS['ts_path']; ?>/<?php echo basename(__FILE__); ?>" method="post">
<div class="wrap">
<h2>Shoots</h2>
<table>
<th width=20%>Name</th>
<th width=20%>Location</th>
<th width=10%>Date</th>
<th width=10%>Events</th>
<th colspan="3">Actions</th>
<?php
$shoots = $wpdb->get_results("select $table_shoots.shootid, $table_shoots.name as shootname, $table_shoots.date, $table_ranges.name as rangename from $table_shoots join $table_ranges on $table_shoots.rangeid=$table_ranges.rangeid order by date asc");
if ($shoots === FALSE) {
   die($wpdb->print_error());
}
foreach($shoots as $shoot)
{
	echo "<tr><td>" . $shoot->shootname . "</td>\n";
	echo "<td>" . $shoot->rangename . "</td>\n";
	echo "<td>" . $shoot->date . "</td>\n";
    	$query = $wpdb->prepare("select $table_eventtypes.name from $table_eventtypes join $table_events on $table_eventtypes.eventtypeid = $table_events.eventtypeid where $table_events.shootid=%d", $shoot->shootid);
	$events = $wpdb->get_results($query);
	$numrows = $wpdb->query($query); //Is there a better way to get the number of rows rather than 'querying' twice?
	$count=0;
	$str = "";
	foreach($events as $event)
	{
		$str .= $event->name;
		$count++;
		if($count < $numrows)
			$str .= ",";
	}
	echo "<td>" . $str . "</td>\n";
	echo "<td><a href=\"admin.php?page=". $GLOBALS['ts_path'] . "/" . basename(__FILE__) . "&amp;shoots_shootid=" . $shoot->shootid . "&amp;action=edit\" class=\"edit\">Edit</a></td>";
	echo "<td><a href=\"admin.php?page=". $GLOBALS['ts_path'] . "/" . basename(__FILE__) . "&amp;shoots_shootid=" . $shoot->shootid . "&amp;action=remove\" class=\"delete\">Remove</a></td>";
	echo "<td><input type=\"checkbox\" name=\"shoots_shootids[]\" value=\"" . $shoot->shootid. "\"></td></tr>\n";
}
?>
</table>
<div class="submit">
<input type="submit" class="delete" name="action" value="Remove Selected Shoots" />
</div>
</div>
</form>

<form action="admin.php?page=<?php print $GLOBALS['ts_path'] ?>/<?php echo basename(__FILE__); ?>" method="post">
<div class="wrap">
<h2>Add a New Shoot:</h2>
<div>
<label for="name">Name:</label>
<input type="text" name="shoots_name" id="name" value="name" size="64" /><br />
<label for="range">Range:</label>
<select name="shoots_rangeid" id="range">
<?php
	$ranges = $wpdb->get_results("select $table_ranges.rangeid, $table_ranges.name from $table_ranges");
	if ($ranges === FALSE) 
	{
		die($wpdb->print_error());
	}   
	foreach($ranges as $range)
	{
		echo "<option value=" . $range->rangeid . ">" . $range->name . "</option>\n";
	}
?>
</select><br />
Events:<br/>
<?php
	$eventtypes = $wpdb->get_results("select $table_eventtypes.eventtypeid, $table_eventtypes.name from $table_eventtypes");
	if ($eventtypes === FALSE) 
	{
		die($wpdb->print_error());
	} 
	foreach($eventtypes as $eventtype)
	{
		echo "<input type=\"checkbox\" name=\"" . $eventtype->eventtypeid . "\" value=1 />" . $eventtype->name . "<br />\n";
	}
?>
<input type="hidden" name="action" value="addshoot" />
</div>
<div class="submit"><input type="submit" value="Add Shoot &raquo;" /></div>
</div>
</form>
</div>
